package com.rbcs.flow.webapp;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.rbcs.flow.FlowConstants;
import com.rbcs.flow.model.Flow;
import com.rbcs.flow.service.FlowManager;
import com.systop.common.modules.security.user.UserUtil;
import com.systop.core.ApplicationException;
import com.systop.core.webapp.struts2.action.JsonCrudAction;

/**
 * 审批流程管理Action
 * 
 * @author ShangHua,Nice
 * 
 */
@SuppressWarnings("serial")
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class FlowAction extends JsonCrudAction<Flow, FlowManager> {

	// JSON数据
	private Map<String, String> auditResult;

	// 开始时间
	private Date startDate;

	// 结束时间
	private Date endDate;

	// 单号
	private String singleNo;

	// 公用查询语句
	private StringBuffer hql = new StringBuffer("from Flow f ");

	/**
	 * 预算申请,会计操作
	 * 
	 * @return
	 */
	public String budgetIndex() {
		hql.append("where f.budget is not null ");
		commonAccountantQuery(hql, FlowConstants.FLOW_BUDGET);
		return "budget_index";
	}


	/**
	 * 公用会计待审查询条件封装,会计操作
	 */
	private void commonAccountantQuery(StringBuffer hql, String type) {
		// List<Object> args = new ArrayList<Object>();
		// // 查询部门审核已结束的流程
		// hql.append("and f.status = ? ");
		// args.add(FlowConstants.FLOW_END);
		// if (StringUtils.isNotBlank(getModel().getFinanceAudStatus())) {
		// hql.append("and f.financeAudStatus = ? ");
		// args.add(getModel().getFinanceAudStatus());
		// }
		// commonQueryHql(hql, args, type);
		// hql.append("order by f.financeAudStatus asc, f.createDate asc");
		// page = PageUtil.getPage(getPageNo(), getPageSize());
		// page = getManager().pageQuery(page, hql.toString(), args.toArray());
		//
		// restorePageData(page);
	}

	/**
	 * 公用查询条件拼装
	 */
	@SuppressWarnings("unused")
	private void commonQueryHql(StringBuffer hql, List<Object> args, String type) {
//		if (StringUtils.isNotBlank(singleNo)) {
//			if (FlowConstants.FLOW_BORROW.equals(type)) {
//				hql.append("and f.borrow.singleNo like ? ");
//			} else if (FlowConstants.FLOW_REPAY.equals(type)) {
//				hql.append("and f.rePay.singleNo like ? ");
//			} else if (FlowConstants.FLOW_REIMB.equals(type)) {
//				hql.append("and f.reimb.singleNo like ? ");
//			} else if (FlowConstants.FLOW_BUDGET.equals(type)) {
//				hql.append("and f.budget.singleNo like ? ");
//			}
//			args.add(MatchMode.ANYWHERE.toMatchString(singleNo));
//		}
//		if (startDate != null) {
//			hql.append("and f.createDate >= ? ");
//			args.add(startDate);
//		}
//		if (endDate != null) {
//			hql.append("and f.createDate <= ? ");
//			args.add(endDate);
//		}
	}

	/**
	 * 会计审核操作[借款、报销、预算]
	 */
	public String saveAuditResult() {
		auditResult = new HashMap<String, String>();
		try {
			getModel().setAuditor(UserUtil.getLoginUser(getRequest()));
			getManager().saveAuditResult(getModel());
			auditResult.put("result", "success");
		} catch (ApplicationException e) {
			auditResult.put("result", "error");
			auditResult.put("msg", e.getMessage());
		}
		return "jsonRst";
	}

	/**
	 * 显示审核状态
	 */
	public Map<String, String> getAuditStatusMap() {
		return FlowConstants.AUDIT_STATUS_MAP;
	}



	/**
	 * return
	 */

	public Map<String, String> getAuditResult() {
		return auditResult;
	}

	public void setAuditResult(Map<String, String> auditResult) {
		this.auditResult = auditResult;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getSingleNo() {
		return singleNo;
	}

	public void setSingleNo(String singleNo) {
		this.singleNo = singleNo;
	}

}
